# https://www.runoob.com/python3/python3-inputoutput.html

print("######  格式化输出  ######")

print("x".rjust(5))
print(repr(1*2).rjust(5))
print(repr(1*2).rjust(10,"0"))
print(repr(1*2).center(10,"@"))

for x in range(1, 4):
    print('{0:2d} {1:3d} {2:5d}'.format(x, x*x, x*x*x))

print("10".zfill(5))

print('{}网址： "{}!"'.format('菜鸟教程', 'www.runoob.com'))

# 位置参数， 关键字参数 可以混合一起使用
print('站点列表 {0}, {1}, 和 {other}。'.format('Google', 'Runoob', other='Taobao'))

table = {'Google': 1, 'Runoob': 2, 'Taobao': 3}
for name, number in table.items():
    print('{0:10} ==> {1:10d}'.format(name, number))


print('Runoob: {0[Runoob]:d}; Google: {0[Google]:d}; Taobao: {0[Taobao]:d}'.format(table))

print('Runoob: {Runoob:3d}; Google: {Google:3d}; Taobao: {Taobao:3d}'.format(**table))

import math
print('常量 PI 的值近似为 :{:10.3f} '.format(math.pi))
print('常量 PI 的值近似为 :{0:10.3f} '.format(math.pi))

print("######  读和写文件  ######")

import os
# 打印当前工作目录
print("当前工作目录:", os.getcwd())

f = open("learn-python-basic/test_for_read.txt", "r")
print("当前文件指针位置：",f.tell())
conent = f.read()  # 读取所有内容
print("当前文件指针位置：",f.tell())
print(conent)
f.close()


f = open("learn-python-basic/test_for_write.txt", "w")
num=f.write("Hello World! \n")
print("写入的字符数:", num)
f.close()

f = open("learn-python-basic/test_for_write.txt", "a")
f.write("Append:: Hello World!")
f.close()

print("######  循环按行读取文件  ######")
f = open("learn-python-basic/test_for_read.txt", "r")
while True:
    line = f.readline()  # 读取一行
    print(line)
    if not line:
        break
f.close()

# f.readlines() 读取所有行
f = open("learn-python-basic/test_for_read.txt", "r")
print(f.readlines())

# 迭代文件对象，循环读取每行
f = open("learn-python-basic/test_for_read.txt", "r")
for line in f:
    print(line, end='')
f.close()
print()

f = open('learn-python-basic/test_for_seek.txt', 'rb+')
#f.write(b'abcdefghigk')
print(f.seek(1,0))
print(f.read(2))
